// 删除数组中指定的(传入)的某个元素

Array.prototype.remove = function (val) {
    var index = this.indexOf(val) //通过indexOf查询到传入的元素在数组中的索引
    if (index > -1) {
        this.splice(index, 1) //splice删除从index开始,删除一个
    }
}

//eg
var arr = [1, 2, 3, 4, 5, 6, "a", "b"]
arr.remove("a")
console.log(arr) //[1, 2, 3, 4, 5, 6, "b"]

//深克隆

function deepClone(val) {
    //进来先判断val是数组还是对象
    let cloneResult = Array.isArray(val) ? [] : {}
    //然后遍历val
    for (key in val) {
        //判断val中是否含有key属性
        if (val.hasOwnProperty(key)) {
            //有key就进一步判断这个key是否又是一个对象
            if (typeof val[key] === "object") {
                //是对象就递归一次
                cloneResult[key] = deepClone(val[key])
            } else {
                cloneResult[key] = val[key]
            }
        }
    }
    return cloneResult
}
function test(x) {
    var sum = x;
    var mod = function (y) {
        sum = sum * y;
        return mod;
    };
    mod.toString = function () {
        return sum;
    }; return mod;
}
console.log(test(2)(3)(4)(5));
//eg
var arr1 = [1, 2, 3, [4, 5]]
var arr2 = deepClone(arr1)
arr2[3][1] = "修改"
console.log(arr1) //[ 1, 2, 3, [ 4, 5 ] ]
console.log(arr2) //[ 1, 2, 3, [ 4, '修改' ] ]

//获得传入的最大值
function isMax() {
    var max = arguments[0]
    for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] > max) {
            max = arguments[i]
        }
    }
    return max
}

//三个数之和等于target
var threeSum = function (nums, target) {
    let arr = [];
    for (let i = 0; i < nums.length - 2; i++) {
        for (let j = i + 1; j < nums.length - 1; j++) {
            for (let k = i + 2; k < nums.length; k++) {
                if (nums[i] + nums[j] + nums[k] == target) {
                    arr.push(i, j, k)
                    return arr
                }
            }
        }
    }
}


let nums = [1, 2, 3, 4]
console.log(threeSum(nums, 7))

//数组中有对象,需要删除特定对象中某项
var test = [{
    "name": "Mike",
    "incentives": "23.45",
    "id": "1"
},
{
    "name": "Larsen",
    "incentives": "34.78",
    "id": "2"
},
{
    "name": "Steve",
    "incentives": "26.78",
    "id": "3"
}
];

//Use splice to remove the element
test.splice(
    //find the index of the element to be removed
    test.indexOf(test.find(function (element) { return element.id === "2"; }))
    //remove 1 element from the index found
    , 1
);
console.log(test);
